<template>
  <!--会签管理-单据申请-工程项目合同审批弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row style="margin-bottom: 20px;">
          <a-col :span="24">
            <span style="font-size: 20px;font-weight: bold;">工程项目合同审批</span>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title"
              style="margin-bottom: 10px;">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depart_id"
              style="margin-bottom: 10px;">
              <a-select :options="departList" placeholder="请选择申请部门" v-model="model.depart_id">
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId"
              style="margin-bottom: 10px;">
              <a-select :options="levelOneOptions" placeholder="请选择项目" v-model="model.projectId">
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name"
              style="margin-bottom: 10px;">
              <a-input v-model="model.name" placeholder="请输入合同名称"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deadline"
              style="margin-bottom: 10px;">
              <div style="display: flex;align-items: center;">
                 <a-input style="width: calc(100% - 40px);" v-model="model.deadline" placeholder="请输入合同期限"></a-input><div style="width: 40px;">个月</div>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number"
              style="margin-bottom: 10px;">
              <a-input v-model="model.number" placeholder="请输入合同份数"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other"
              style="margin-bottom: 10px;">
              <a-input v-model="model.other" placeholder="请输入对方单位名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money"
              style="margin-bottom: 10px;">
              <a-input v-model="model.money" placeholder="请输入合同金额"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherAddress"
              style="margin-bottom: 10px;">
              <a-input v-model="model.otherAddress" placeholder="请输入对方单位地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNumber"
              style="margin-bottom: 10px;">
              <a-input v-model="model.contractNumber" placeholder="请输入合同编号"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherPhone"
              style="margin-bottom: 10px;">
              <a-input v-model="model.otherPhone" placeholder="请输入对方单位电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime"
              style="margin-bottom: 10px;">
              <a-date-picker placeholder="请选择开始日期" format="YYYY-MM-DD" v-model="model.startTime"
                style="width:100%;" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="合同备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;">
              <textarea v-model="model.remark" cols="100" rows="3" style="width: 83%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同协议附件资料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment"
              style="margin-bottom: 10px;">
              <j-upload v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="pageType === 'add'">
            <div style="display: flex;justify-content: space-between;align-items: center;margin-right: 40px;">
              <div style="line-height: 20px;color: red;">1.请附会签完成的询价单；<br />2.请附对方营业执照、资质证书等相关材料。</div>
              <a-button @click="downfile" type="primary">下载合同模板</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },

    },
    data() {
      return {
        model: {

        },
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 14
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        confirmLoading: false,
        // validatorRules: { paymentMoney: [{required: true,pattern: /^[0-9]{99}$/,message: '请填写金额'}]},
        levelOneOptions: [],
        departList: [], //部门列表
        treeData: [],
        fileList: [],
        countersignTemplateId: '',
        url: {
          add: '/countersign/wtCountersignProjectContract/add',
          edit: '/wtBrand/edit',
          queryById: '/wtBrand/queryById',
        },
        pageType: ''
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      if (!!window.localStorage.getItem("pro__USER_DEPARTLIST")) {
        let departList = JSON.parse(window.localStorage.getItem("pro__USER_DEPARTLIST")).value;
        if (departList && departList.length > 0) {
          this.departList = departList.map((ite) => ({
            ...ite,
            value: ite.id,
            label: ite.departName
          }))
        }
      }
    },
    methods: {
      loadTree() {
        var that = this
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {

            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }

            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })


          }
        })
      },
      add(record, type) {
        console.log(22, record)
        if (!!type) {
          this.pageType = type;
        }
        this.countersignTemplateId = record.id;
        this.edit(this.modelDefault)
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.visible = true
        this.loadTree()
      },
      save() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate((valid) => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            // if (!this.model.id) {
            //   httpurl += this.url.add
            //   method = 'post'
            // } else {
            //   httpurl += this.url.edit
            //   method = 'put'
            // }
            that.model.status = '0'
            // console.log('fde',that.model);
            // debugger
            httpAction('/countersign/wtCountersignPaymentRequest/add', this.model, 'POST')
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                  that.$emit('error')
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }
        })
      },
      handleOk() {
        const that = this
        // 触发表单验证
        // this.$refs.form.validate((valid) => {
        //   if (valid) {
        //     that.confirmLoading = true
        //     let httpurl = ''
        //     let method = ''
        //     if (!this.model.id) {
        //       httpurl += this.url.add
        //       method = 'post'
        //     } else {
        //       httpurl += this.url.edit
        //       method = 'put'
        //     }
        //     httpAction(httpurl, this.model, method)
        //       .then((res) => {
        //         if (res.success) {
        //           that.$message.success(res.message)
        //           that.$emit('ok')
        //         } else {
        //           that.$message.warning(res.message)
        //         }
        //       })
        //       .finally(() => {
        //         that.confirmLoading = false
        //       })
        //   }
        // })
      },
      downfile() {
        this.$emit('待更新')
      },
      submitForm(record) {
        const that = this

        if (!this.model.attachment) {
          that.$message.warning('合同协议附件资料不能为空,请上传')
        } else {
          // 触发表单验证
          this.$refs.form.validate((valid) => {
            if (valid) {
              that.confirmLoading = true
              this.model.status = record;
              let httpurl = ''
              let method = ''
              if (!this.model.id) {
                httpurl += this.url.add
                method = 'post'
              } else {
                httpurl += this.url.edit
                method = 'put'
              }
              that.model.countersignTemplateId = this.countersignTemplateId;
              httpAction(httpurl, this.model, method)
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                    that.$emit('error')
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })

        }
      },
    },
  }
</script>